package pervasivePositioning.ui;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Checkable;

public class OnStrategyChangedListener implements OnClickListener {
	View alternatives[];
	StrategyController controller;
	
	public OnStrategyChangedListener(StrategyController c, View[] a) {
		controller = c;
		alternatives = a;
	}
	
	@Override
	public void onClick(View v) {
		setAlternative(v);
	}
	
	private void setAlternative(View selected)
	{
	    //Cycle through all options
/*		for (View a : alternatives) {
			CheckBox radio = (CheckBox) a;
			if (radio != null) {
		            //Check or uncheck as needed
		            radio.setChecked(selected == radio);
			}
	    }*/
		boolean isChecked = ((Checkable) selected).isChecked();
		controller.changeStrategy(getViewIndex(selected), isChecked);
	}

	private int getViewIndex(View v) {
		for (int i = 0; i < alternatives.length; i++) {
			if (alternatives[i] == v) {
				return i;
			}
		}
		return 0;
	}
	
}
